import { getToken, setToken } from "@/utils/strong";
import { LoginApi } from "@/api/login";

const state = {
  token: getToken(),
  user: "",
};
const mutations = {
  hanglelogin(state, newtoken) {
    // console.log(newtoken);
    state.token = newtoken;
    setToken(newtoken);
  },
  setUser(state, user) {
    state.user = user;
  },
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo;
  },
  removeToken() {
    state.token = null;
  },
  removeUserInfo() {
    state.user = "";
  },
};
const actions = {
  async loginInfo(ctx, data) {
    // console.log(ctx);
    // console.log(data);
    const res = await LoginApi(data);
    console.log(res);
    ctx.commit("hanglelogin", res.data.data.token.token);
    ctx.commit("setUser", res.data.data.user);
    // this.$store.state.token = res.data.data.token;
  },
  removeUserInfo(ctx) {
    ctx.commit("removeToken");
    ctx.commit("removeUserInfo");
  },
};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
